
local sessionTcp = class("session", z9.sessionBase)

function sessionTcp:ctor(conn)
    self.super.ctor(self, conn)

    self._conn = conn
    self._index = 1
    self._actions = {}
end 

function sessionTcp:close()
    self._conn:close()
end 

function sessionTcp:send(tabMsg)
    
end 

function sessionTcp:sendResp(tabMsg)
    z9.log.Info("sendResp: " .. z9.json.encode(tabMsg))
    local msg = self._funcResp(tabMsg)

    local data = msg
    local hex = string.bin2hex(data)
    z9.log.Info(hex)

    local buff = buffer.new()
    packet.writer(buff):writeStr(msg)

    local data = clone(buff:content())
    local hex = string.bin2hex(data)
    z9.log.Info(hex)

    return self._conn:send(buff)
end 

function sessionTcp:sendReq(strCmd, tabMsg)
    local msg = proto.encode(strCmd, tabMsg, self._index)
    self:addAction(self._index, strCmd)

--    self._index = self._index + 1
--    local buff = buffer.new(msg)
--    log.Info("send Req before")
--    --return self._conn:send(buff)
--    self._conn:sendDelay(buff)
--    log.Info("send Req after")
    local buff = buffer.new()
    packet.writer(buff):writeStr( msg )
    self._conn:send(buff)
    --self._conn:send(buffer.new(json.encode(tabMsg)))
end 

function sessionTcp:sendbuff(buff)
    return self._conn:send(buff)
end

function sessionTcp:setResp(funcResp)
    self._funcResp = funcResp
end 

function sessionTcp:addAction(index, actionName)
    self._actions[index] = actionName
    self._index = self._index + 1
end 

function sessionTcp:getActionName(index)
     local actionName = self._actions[index]
     self._actions[index] = nil
     z9.log.Info("getActionName: " .. actionName)
     return actionName
end 

function sessionTcp:flush()
    self._conn:flush()
end 

function sessionTcp:close()
    self._conn:close()
end 


return sessionTcp
